/******************************************************************
 * Copyright (c) 2007 - 2007, Simon Guerard, All rights reserved.
 * 
 * Projet de jeu de course au large.
 * 
 * This File is part of Sail-Race
 * 
 * Sail-Race is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
 * 
 * Sail-Race is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with Sail-Race; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */
package net.simcafamily.sailrace.core;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;

/**
 * Classe utilitaire pour la creation d'interfaces graphiques.
 * 
 * @author Romain Guy
 */
public final class UIHelper {

    /**
     * Cree un bouton muni d'un texte.
     * 
     * @param text
     *            Le titre du bouton
     * @return Le nouveau bouton
     * @author Romain Guy
     */
    public static JButton createButton(final String text) {
        final JButton button = new JButton(text);
        button.setFocusPainted(true);
        button.setBorderPainted(true);
        button.setContentAreaFilled(true);
        return button;
    }

    /**
     * Cree un bouton muni d'une image et d'un texte. L'image change lors du survol de la souris.
     * 
     * @param text
     *            Le titre du bouton
     * @param icon
     *            L'icone associee au bouton. Les icons icon.png, icon_highlight.png et icon_pressed.png doivent se
     *            trouver dans images/.
     * @return Le nouveau bouton
     * @author Romain Guy
     */
    public static JButton createButton(final String text, final String icon) {
        return UIHelper.createButton(text, icon, false);
    }

    /**
     * Cree un bouton muni d'une image et d'un texte. L'image change lors du survol de la souris.
     * 
     * @param text
     *            Le titre du bouton
     * @param icon
     *            L'icone associee au bouton. Les icons icon.png, icon_highlight.png et icon_pressed.png doivent se
     *            trouver dans images/.
     * @param flat
     *            Si vrai, le bouton sera plat
     * @return Le nouveau bouton
     * @author Romain Guy
     */
    public static JButton createButton(final String text, final String icon, final boolean flat) {
        final ImageIcon iconNormal = UIHelper.readImageIcon(icon + ".png");
        final ImageIcon iconHighlight = UIHelper.readImageIcon(icon + "_highlight.png");
        final ImageIcon iconPressed = UIHelper.readImageIcon(icon + "_pressed.png");

        final JButton button = new JButton(text, iconNormal);
        button.setFocusPainted(!flat);
        button.setBorderPainted(!flat);
        button.setContentAreaFilled(!flat);
        if (iconHighlight != null) {
            button.setRolloverEnabled(true);
            button.setRolloverIcon(iconHighlight);
        }
        if (iconPressed != null) {
            button.setPressedIcon(iconPressed);
        }
        return button;
    }

    /**
     * Place le composant fourni en parametre au centre de l'ecran.
     * 
     * @param component
     *            Le composant a centrer par rapport a l'ecran *@author Romain Guy
     */
    public static void centerOnScreen(final Component component) {
        final Dimension paneSize = component.getSize();
        final Dimension screenSize = component.getToolkit().getScreenSize();
        component.setLocation((screenSize.width - paneSize.width) / 2, (screenSize.height - paneSize.height) / 2);
    }

    /**
     * Charge le fichier specifie sous forme d'icone.
     * 
     * @param filename
     *            Le nom du fichier image a charger depuis le package fr.capgemini.salesmanager.images
     * @return Un objet icone
     * @author Romain Guy
     */
    public static ImageIcon readImageIcon(final String filename) {

        final URL url = UIHelper.class.getResource("/images/" + filename);
        if (url == null) {
            return null;
        }

        return new ImageIcon(Toolkit.getDefaultToolkit().getImage(url));
    }
}
